<?php
declare (strict_types = 1);

namespace app\admin\model;

use Symfony\Component\Finder\SplFileInfo;
use think\Model;

/**
 * @mixin \think\Model
 */
class AdminPhoto extends Model
{
    protected $table = 'admin_photo';
    // 定义时间戳字段名
    public function getTypeAttr($value)
    {
        $type = ['1' => '本地', '2' => '阿里云'];
        return $type[$value];
    }

    public function getHrefAttr($value)
    {
        if(substr($value, 0, 4) != 'http'){
            return 'http://'.request()->host().'/'.$value;
        }else{
            return $value;
        }
    }


    public function add($info,$href,$type,$mime)
    {
        $data = [
            'name' => $info->getFilename(),
            'href' => $href,
            'type' => $type,
            'ext' => $info->getExtension(),
            'mime' => $mime,
            'size' => $info->getSize(),
        ];
        self::create($data);
    }

    static public function del($id)
    {
        $photo =  self::find($id);
        if($photo['type']=='阿里云'){
//            alYunDel($photo['href']);
        }else{
            //删除本地文件
            $path = '../public'.$photo['href'];
            if (file_exists($path)) unlink($path);
        }
        $photo->delete();
    }
}